﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using lite;
using System.Data.SqlClient;
using Csla.Data;
using Csla;
using OpenExpressApp;
using System.Collections;
using Csla.Core;
using OpenExpressApp.ORM;
using OpenExpressApp.MetaAttribute;
using OpenExpressApp.DB.Utils;
using OpenExpressApp.Types;
using OpenExpressApp.Command;
using OpenExpressApp.MetaModel;

namespace OpenExpressApp.Module.Library
{
    public partial class BoInfoOperationSelectedList : GBusinessListBase<BoInfoOperationSelectedList, BoInfoOperationSelected>
    {
        public BoInfoOperationSelectedList()
        {
            AllowNew = true;
        }

        /// <summary>
        /// 根据类型来获取Command列表
        /// </summary>
        /// <param name="boType"></param>
        /// <returns></returns>
        public static BoInfoOperationSelectedList GetList(BusinessObjectInfo boInfo)
        {
            return DataPortal.Fetch<BoInfoOperationSelectedList>(boInfo);
        }

        [RunLocal]
        private void DataPortal_Fetch(BusinessObjectInfo boInfo)
        {
            RaiseListChangedEvents = false;

            //允许查看对象
            this.Add(new BoInfoOperationSelected(CanViewObjectCommandMock.Instance, boInfo));

            var toolbarType = boInfo.IsDefaultObject ? ToolbarType.Main : ToolbarType.Child;

            var commands = ApplicationModel.Commands.Where(c =>
                c.AutoExecuting == false &&//非自动启动的命令
                (c.TargetObjectType == null || c.TargetObjectType == boInfo.BOType) &&//TargetObjectType是指定的类型
                (c.ToolbarType == ToolbarType.Any || c.ToolbarType == toolbarType) &&//指定的ToolbarType
                ApplicationModel.IsNotVisibleCommand(c.Id, boInfo) == false//过滤不可见的command
                );

            if (boInfo.IsDefaultObject)
            {
                ModuleType moduleType = boInfo.DefaultObjectAttribute.ModuleType;
                commands = commands.Where(c => c.ModuleType == ModuleType.Unspecified || c.ModuleType == moduleType);
            }

            foreach (var command in commands)
            {
                this.Add(new BoInfoOperationSelected(command, boInfo));
            }

            RaiseListChangedEvents = true;
        }

        private class CanViewObjectCommandMock : OpenExpressApp.Command.CommandBase
        {
            public static CanViewObjectCommandMock Instance = new CanViewObjectCommandMock()
            {
                Id = CommandNames.CanViewObject,
                Label = "允许查看对象（系统功能）",
            };

            public override void Execute(object param)
            {

            }
        }

        /// <summary>
        /// 根据整个聚合对象集的Command列表
        /// </summary>
        /// <param name="boType"></param>
        /// <returns></returns>
        public static BoInfoOperationSelectedList GetListInPermission(BusinessObjectInfo boInfo)
        {
            BoInfoOperationSelectedList result = new BoInfoOperationSelectedList();
            foreach (var item in boInfo.ObjectInfoListInPermission)
            {
                BoInfoOperationSelectedList list = DataPortal.Fetch<BoInfoOperationSelectedList>(item);
                result.AddRange(list);
                list.Clear();  //去除item事件对list的引用，以免内存泄露
            }
            return result;
        }
    }

    [Serializable]
    [BusinessObject("2F1C87E1-8067-49b5-918C-230038AFA1F7", PropertyGroup = "ObjectName"), Label("对象功能")]
    public partial class BoInfoOperationSelected : GBusinessBase<BoInfoOperationSelected>, IDisplayModel
    {
        public BoInfoOperationSelected(OpenExpressApp.Command.CommandBase cmd, BusinessObjectInfo boInfo)
        {
            Operation = new Operation(cmd);
            this.BusinessObjectInfo = boInfo;
            SetProperty<bool>(IsSelectedProperty, false);
        }

        public BusinessObjectInfo BusinessObjectInfo { get; set; }
        [EntityProperty, Label("对象名称")]
        public string ObjectName
        {
            get
            {
                return BusinessObjectInfo.Label;
            }
        }

        private static PropertyInfo<bool> IsSelectedProperty =
            RegisterProperty(new PropertyInfo<bool>("IsSelected"));
        [EntityProperty]
        [ShowInList, Label("选择"), ShowInLookup]
        public bool IsSelected
        {
            get { return GetProperty(IsSelectedProperty); }
            set { SetProperty(IsSelectedProperty, value); }
        }

        public Operation Operation { get; set; }
        [EntityProperty, Label("功能名称")]
        public string OperationName
        {
            get
            {
                return Operation.Name;
            }
        }

        [Indexable]
        public Guid BusinessObjectId
        {
            get { return BusinessObjectInfo.Id; }
        }

        [Indexable]
        public Guid OperationId
        {
            get { return Operation.Id; }
        }
    }
}